﻿using AutoMapper;
using MediatR;
using Traceability.Doamin;
using Traceability.ErrorCode;
using Traceability.Infratruvture.Interfares;
using Traceability.Write.Api.Application.Command.CommodityCommand;

namespace Traceability.Write.Api.Application.CommandHander.CommodityHander
{
    public class DelCommodityHander : IRequestHandler<DelCommodityCommand, ApiResult<int>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRequsitory<Commodity> baseRequsitory;

        public DelCommodityHander(IMapper mapper, IBaseRequsitory<Commodity> baseRequsitory)
        {
            this.mapper = mapper;
            this.baseRequsitory = baseRequsitory;
        }

        /// <summary>
        /// 删除商品操作
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(DelCommodityCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var list = baseRequsitory.GetAll().Where(x => x.CommodityId == request.CommodityId).FirstOrDefault();

            baseRequsitory.Del(list);

            result.Code = ApiEnum.删除成功;
            result.Msg = ApiEnum.删除成功.ToString();
            return Task.FromResult(result);
        }
    }
}
